﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Matrix.ViewModel;

namespace Matrix.GUI
{
   /// <summary>
   /// Interaction logic for Matrix.xaml
   /// </summary>
   public partial class MatrixControl : UserControl
   {
      public MatrixControl()
      {
         InitializeComponent();
      }

      ElementControl getElementControl(int row, int column)
      {
         return matrixGrid.Children.Cast<ElementControl>().First(e => Grid.GetRow(e) == row && Grid.GetColumn(e) == column);
      }

      private void UserControl_Loaded(object sender, RoutedEventArgs e)
      {
         var instance = this.DataContext as MatrixInstance;
         if (instance != null)
         {
            foreach (var element in instance.Elements)
            {
               if (element != null)
               {
                  var control = getElementControl(element.RowPosition, element.ColumnPosition);
                  if (control != null)
                  {
                     control.DataContext = element;
                  }
               }
            }
         }
      }   
   }
}
